
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0064
 * @Author : WangRuoyu 
 * @Date : 2023/5/26 16:08
 */

#include "iostream"
#include "vector"

using namespace std;

class Solution {
public:
    int minPathSum(vector<vector<int>> &grid) {
        int m = grid.size();
        int n = grid[0].size();
        int sum[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                sum[i][j] = 0;
                int tmp = 0x3f3f3f3f;
                if (i - 1 >= 0) tmp = min(tmp, sum[i - 1][j]);
                if (j - 1 >= 0) tmp = min(tmp, sum[i][j - 1]);
                if (tmp > 1e8) tmp = 0;
                sum[i][j] = grid[i][j] + tmp;
            }
        }
        return sum[m - 1][n - 1];
    }
};

int main() {
    Solution sl;
    vector<vector<int>> grid = {{1, 3, 1},
                                {1, 5, 1},
                                {4, 2, 1}};
    printf("%d", sl.minPathSum(grid));
    return 0;
}